package com.mit.web.business;

import java.util.List;

import org.apache.log4j.Logger;

import com.mit.common.RequestContext;
import com.mit.common.ResultContext;
import com.mit.common.dto.PieChartItemDTO;
import com.mit.common.exception.BaseException;
import com.mit.common.maintenance.dto.EmployeeDTO;
import com.mit.common.maintenance.dto.FabricCatDTO;
import com.mit.common.maintenance.dto.OrganizationDTO;
import com.mit.common.maintenance.dto.PaymentDTO;
import com.mit.common.maintenance.dto.ProductColorDTO;
import com.mit.common.maintenance.dto.ProductTypeDTO;
import com.mit.common.maintenance.dto.SeasonDTO;
import com.mit.common.maintenance.dto.UnitDTO;
import com.mit.common.security.dto.UserDTO;
import com.mit.common.token.MaintenanceTokens;
import com.mit.common.token.Tokens;

public class MaintenanceService extends RemoteService {
	
	private static final Logger logger = Logger.getLogger(MaintenanceService.class);
	
	private final String FACADE_NAME = "MaintenanceFacade";
	@Override
	public String getFacadeName() {
		return FACADE_NAME;
	}
	
	/**************** Product Type Maintenance  *******************/
	@SuppressWarnings("unchecked")
	public List<ProductTypeDTO> retrieveProductTypes(UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveProductTypes", currentUser);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<ProductTypeDTO> lst = (List<ProductTypeDTO>) rlt.getResult(MaintenanceTokens.KEY_PRD_TYPE_LIST);
		return lst;
	}
	
	@SuppressWarnings("unchecked")
	public List<ProductTypeDTO> updateProductTypes(List<ProductTypeDTO> prodTypeLst, UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("updateProductTypes", currentUser);
		rqs.addParameter(MaintenanceTokens.KEY_PRD_TYPE_LIST, prodTypeLst);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);		
		List<ProductTypeDTO> lst = (List<ProductTypeDTO>) rlt.getResult(MaintenanceTokens.KEY_PRD_TYPE_LIST);
		return lst;
	}
	
	/**************** Product Color Maintenance  *******************/
	@SuppressWarnings("unchecked")
	public List<ProductColorDTO> retrieveProductColors(UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrieveProductColors", currentUser);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<ProductColorDTO> list = (List<ProductColorDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_COLOR_LIST);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<ProductColorDTO> updateProductColors(List<ProductColorDTO> prodColorList, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("updateProductColors", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_PRD_COLOR_LIST, prodColorList);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<ProductColorDTO> list = (List<ProductColorDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_COLOR_LIST);
		return list;
	}
	
	/**************** Unit Maintenance  *******************/
	@SuppressWarnings("unchecked")
	public List<UnitDTO> retrieveUnits(UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrieveUnits", currentUser);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<UnitDTO> list = (List<UnitDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_UNIT_LIST);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<UnitDTO> updateUnits(List<UnitDTO> prodColorList, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("updateUnits", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_PRD_UNIT_LIST, prodColorList);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<UnitDTO> list = (List<UnitDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_UNIT_LIST);
		return list;
	}
	
	/**************** Fabric Category Maintenance  *******************/
	@SuppressWarnings("unchecked")
	public List<FabricCatDTO> retrieveFabricCats(UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrieveFabricCats", currentUser);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<FabricCatDTO> list = (List<FabricCatDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_FATRIC_CAT_LIST);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<FabricCatDTO> updateFabricCats(List<FabricCatDTO> fabricCatList, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("updateFabricCats", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_PRD_FATRIC_CAT_LIST, fabricCatList);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<FabricCatDTO> list = (List<FabricCatDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_FATRIC_CAT_LIST);
		return list;
	}
		
	/**************** Payment Maintenance  *******************/
	@SuppressWarnings("unchecked")
	public List<PaymentDTO> retrievePayments(UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrievePayments", currentUser);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<PaymentDTO> list = (List<PaymentDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_PAYMENT_LIST);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<PaymentDTO> updatePayments(List<PaymentDTO> paymentList, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("updatePayments", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_PRD_PAYMENT_LIST, paymentList);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<PaymentDTO> list = (List<PaymentDTO>) resultCtx.getResult(MaintenanceTokens.KEY_PRD_PAYMENT_LIST);
		return list;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<SeasonDTO> retrieveSeasons(UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrieveSeasons", currentUser);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);		
		List<SeasonDTO> list = (List<SeasonDTO>) resultCtx.getResult(MaintenanceTokens.KEY_SEASON_LIST);
		return list;

	}
	
	@SuppressWarnings("unchecked")
	public List<EmployeeDTO> retrieveEmployees(UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrieveEmployees", currentUser);
/*		requestCtx.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		requestCtx.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);*/
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);
		List<EmployeeDTO> empList = (List<EmployeeDTO>) resultCtx.getResult(MaintenanceTokens.KEY_EMPLOYEE_LIST);
		return empList;
	}
	
	@SuppressWarnings("unchecked")
	public List<EmployeeDTO> retrieveActiveEmployees(UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrieveActiveEmployees", currentUser);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);
		List<EmployeeDTO> empList = (List<EmployeeDTO>) resultCtx.getResult(MaintenanceTokens.KEY_EMPLOYEE_LIST);
		return empList;
	}

/*	@SuppressWarnings("unchecked")
	public List<EmployeeDTO> retrievePagedEmployees(String empCode, Integer depId, Integer pageNo, Integer pageSize) throws BaseException{
		UserDTO currentUser = new UserDTO();
		currentUser.setAdminInd("admin");
		RequestContext rqs = createRequestContext("retrievePagedEmployees", currentUser);
		rqs.addParameter(Tokens.KEY_PAGE_NO, pageNo);
		rqs.addParameter(Tokens.KEY_PAGE_SIZE, pageSize);
		rqs.addParameter(MaintenanceTokens.KEY_EMPLOYEE_CODE, empCode);
		//if(depId > 0)
		//	rqs.addParameter(MaintenanceTokens.KEY_DEPARTMENT_ID, depId);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);
		List<EmployeeDTO> empLst = (List<EmployeeDTO>) rlt.getResult(MaintenanceTokens.KEY_EMPLOYEE_LIST);
		return empLst;
	}*/
	
	public Integer retrieveEmployeesCount(UserDTO currentUser) throws BaseException{
		RequestContext rqs = createRequestContext("retrieveEmployeeCount", currentUser);
	//	rqs.addParameter(MaintenanceTokens.KEY_EMPLOYEE_CODE, empCode);
		//if(depId > 0)
		//	rqs.addParameter(MaintenanceTokens.KEY_DEPARTMENT_ID, depId);
		ResultContext rlt = ServiceLocator.getInstance().doBusiness(rqs);
		Integer rc = (Integer) rlt.getResult(Tokens.KEY_RECORD_COUNT);
		return rc;
	}
	
	public EmployeeDTO retrieveEmployeeById(Integer empId, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("retrieveEmployeeById", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_EMPLOYEE_ID, empId);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);
		return (EmployeeDTO) resultCtx.getResult(MaintenanceTokens.KEY_EMPLOYEE_DTO);
	}
	
	public EmployeeDTO updateEmployee(EmployeeDTO employee, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("updateEmployee", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_EMPLOYEE_DTO, employee);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);	
		employee = (EmployeeDTO) resultCtx.getResult(MaintenanceTokens.KEY_EMPLOYEE_DTO);
		return employee;
	}
	
	public EmployeeDTO createEmployee(EmployeeDTO employee, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("createEmployee", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_EMPLOYEE_DTO, employee);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);	
		employee = (EmployeeDTO) resultCtx.getResult(MaintenanceTokens.KEY_EMPLOYEE_DTO);
		return employee;
	}
	
	public void deleteEmployee(EmployeeDTO employee, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("deleteEmployee", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_EMPLOYEE_DTO, employee);
		ServiceLocator.getInstance().doBusiness(requestCtx);
	}
	
	public List<PieChartItemDTO>  retrieveDepartmentStatItem() throws BaseException{
		BaseException e = new BaseException("This is a text Exception", new NullPointerException());
		throw e;
		//TODO Dummy code for presentation!
		/*List<PieChartItemDTO> rtn = new ArrayList<PieChartItemDTO>();

		rtn.add(new PieChartItemDTO("DEP", new Double(RandomUtils.nextInt(100))));
		rtn.add(new PieChartItemDTO("HR", new Double(RandomUtils.nextInt(100))));		
		rtn.add(new PieChartItemDTO("SALES", new Double(RandomUtils.nextInt(100))));	
		rtn.add(new PieChartItemDTO("OTHERS", new Double(RandomUtils.nextInt(100))));	
		return rtn;*/
	}
	
	public OrganizationDTO retrieveOrganization(Integer orgId, UserDTO currentUser) throws BaseException{
		logger.debug("orgId=" + orgId);
		RequestContext requestCtx = createRequestContext("retrieveOrganization", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_ORGANIZATION_ID, orgId);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);
		return (OrganizationDTO) resultCtx.getResult(MaintenanceTokens.KEY_ORGANIZATION_DTO);
	}
	
	public OrganizationDTO updateOrganization(OrganizationDTO organization, UserDTO currentUser) throws BaseException{
		RequestContext requestCtx = createRequestContext("updateOrganization", currentUser);
		requestCtx.addParameter(MaintenanceTokens.KEY_ORGANIZATION_DTO, organization);
		ResultContext resultCtx = ServiceLocator.getInstance().doBusiness(requestCtx);	
		organization = (OrganizationDTO) resultCtx.getResult(MaintenanceTokens.KEY_ORGANIZATION_DTO);
		return organization;
	}
}
